/*
 * PACKAGE NAME: com.jodinfo.outmirror.api.loseformbarcode.service
 * FILE    NAME: LoseformBarcodeDaoImpl.java
 * COPYRIGHT: Copyright(c) © 2016 heroj Company Ltd. All Rights Reserved
 */ 
package com.jodinfo.outmirror.api.loseformbarcode.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.hero.core.constant.SysConstant;
import com.hero.core.entity.PageResult;
import com.hero.core.entity.ResponseEntity;
import com.hero.core.exception.AbstractGenericException;
import com.hero.common.web.excel.util.ExcelExport;
import com.hero.common.web.context.UserContext;
import com.hero.common.web.excel.ExcelImportContext;
import com.hero.common.web.excel.IExcelImportData;
import com.hero.core.util.DateUtils;
import com.hero.core.util.StringUtils;
import com.hero.core.util.ValidationUtil;
import com.jodinfo.outmirror.api.loseformbarcode.entity.LoseformBarcodeEntity;

@Service(value="loseformBarcodeBizService")
public class LoseformBarcodeBizServiceImpl implements LoseformBarcodeBizService,IExcelImportData{
    private static final Logger logger = LoggerFactory.getLogger(LoseformBarcodeBizServiceImpl.class);
 	
 	@Resource(name="loseformBarcodeDmlService")
	private LoseformBarcodeDmlService LoseformBarcodeDmlService;
	@Resource(name="loseformBarcodeQueryService")
	private LoseformBarcodeQueryService LoseformBarcodeQueryService;
	
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.loseformbarcode.LoseformBarcodeBizService#addLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity)
	 * @param LoseformBarcodeEntity LoseformBarcodeEntity
	 * @return int
	 */
	public ResponseEntity addLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity) {
			ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
			  	 int i =  LoseformBarcodeDmlService.addLoseformBarcode(LoseformBarcodeEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.loseformbarcode.LoseformBarcodeBizService#updateLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity)
	 * @param LoseformBarcodeEntity LoseformBarcodeEntity
	 * @return ResponseEntity
	 */
	public ResponseEntity updateLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
				int i = LoseformBarcodeDmlService.updateLoseformBarcode(LoseformBarcodeEntity);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.loseformbarcode.LoseformBarcodeBizService#queryAllLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity)
	 * @param LoseformBarcodeEntity LoseformBarcodeEntity
	 * @return String
	 */
	public String queryAllLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity) {
		return JSON.toJSONString(LoseformBarcodeQueryService.queryAllLoseformBarcode(LoseformBarcodeEntity));
	} 
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.loseformbarcode.LoseformBarcodeBizService#queryAllLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity)
	 * @param LoseformBarcodeEntity LoseformBarcodeEntity
	 * @return PageResult
	 */
	public String queryListForPageLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity) {
		return new PageResult( LoseformBarcodeQueryService.queryListForPageLoseformBarcode(LoseformBarcodeEntity), LoseformBarcodeQueryService.queryCountLoseformBarcode(LoseformBarcodeEntity)).toString();
	} 
	/** 
	 * 
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.loseformbarcode.LoseformBarcodeQueryService#queryById(LoseformBarcodeEntity LoseformBarcodeEntity)
	 * @param BigInteger id
	 * @return
	 */
	public LoseformBarcodeEntity queryById(BigInteger id) {
		return LoseformBarcodeQueryService.queryById(id);
	}
	
	public void exportLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity, HttpServletResponse response) {
		List<LoseformBarcodeEntity> exportList = null;
		String ids = LoseformBarcodeEntity.getIds();
		if (ValidationUtil.isNullOrEmptyString(ids)) {
			exportList =LoseformBarcodeQueryService.queryAllLoseformBarcode(LoseformBarcodeEntity);
		} else {
			String[] idArray = ids.split(",");
			List<BigInteger> idList = new ArrayList<BigInteger>();
			for(String id : idArray) {
				idList.add(new BigInteger(id));
			}
			exportList = LoseformBarcodeQueryService.queryByIds(idList);
			// 将字典中的code转换为name
		   //convertCodeToName(exportList);
		}
		ExcelExport.exportExcel(exportList, "LoseformBarcodeEntityExcel", "LoseformBarcodeEntity信息", response);
	}
	
	/** 
	 * 批量删除 数据处理
	 * @author weiwei
	 * @date 2016年12月30日 上午9:15:36
	 * @see com.jodinfo.outmirror.implementation.loseformbarcode.LoseformBarcodeQueryService#queryById(LoseformBarcodeEntity LoseformBarcodeEntity)
	 * @param BigInteger id
	 * @return
	 */
	public ResponseEntity batchDelLoseformBarcode(LoseformBarcodeEntity LoseformBarcodeEntity) {
		String ids = LoseformBarcodeEntity.getIds();
		String[] idArray = ids.split(",");
		List<LoseformBarcodeEntity> LoseformBarcodeEntityList = new ArrayList<LoseformBarcodeEntity>();
		for(String id : idArray) {
			LoseformBarcodeEntity LoseformBarcodeModel = new LoseformBarcodeEntity();
			LoseformBarcodeModel.setId(new BigInteger(id));
			LoseformBarcodeModel.setUpdateTime(DateUtils.getCurrentTime());
			LoseformBarcodeModel.setIsDel(SysConstant.IS_DEL_Y);
			LoseformBarcodeEntityList.add(LoseformBarcodeModel);
		}
		return batchUpdateLoseformBarcode(LoseformBarcodeEntityList);
	}
	
	/**
	 * 执行批量删除操作
	 * @author weiwei
	 * @date 2017年3月13日 上午11:40:31
	 * @param LoseformBarcodeList
	 * @return
	 */
	public ResponseEntity batchUpdateLoseformBarcode(List<LoseformBarcodeEntity> LoseformBarcodeList) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
			try{
				int i = LoseformBarcodeDmlService.batchUpdateLoseformBarcode(LoseformBarcodeList);
				 rs.setMessage(i+"条记录操作成功");
			 }catch(AbstractGenericException e){
				logger.error(e.getFullMessage());
				rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
			}catch(Exception e){
				String errorMessage = StringUtils.toString(e);
				logger.error(errorMessage);
				rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
			}
		return rs;
	}
	
	/**
	 * 
	 * @author weiwei
	 * @date 2017年3月13日 上午10:24:53
	 * @see  com.jodinfo.outmirror.implementation.loseformbarcode.LoseformBarcodeQueryService#queryById(LoseformBarcodeEntity LoseformBarcodeEntity)
	 * @param map
	 * @param excelimportcontext
	 * @param s
	 * @return
	 * @throws Exception
	 */
	/*public List<BigInteger> insertData(Map<Integer, List<Object>> map,ExcelImportContext excelimportcontext, String s) throws Exception {
		String usrName = UserContext.getUser().getName();//数据创建人
		String creatTime = DateUtils.getCurrentTime();
		List<LoseformBarcodeEntity> LoseformBarcodeList = validateLoseformBarcodeData(map, excelimportcontext,usrName,creatTime);
		if (!excelimportcontext.hasError() && LoseformBarcodeList.size() > 0) {
			batchAddLoseformBarcode(LoseformBarcodeList);
		}
		return null;
	}*/
	
	/**
	 * 
	 * TODO 验证数据 处理业务数据
	 * @author weiwei
	 * @date 2017年3月13日 上午9:33:30
	 * @see  com.jodinfo.outmirror.implementation.loseformbarcode.LoseformBarcodeQueryService#queryById(LoseformBarcodeEntity LoseformBarcodeEntity)
	 * @param LoseformBarcodeList
	 * @return
	 */
	/*public List<LoseformBarcodeEntity> validateLoseformBarcodeData(Map<Integer, List<Object>> map,ExcelImportContext excelimportcontext,String usrName,String creatTime) {
		ExcelImportContext.DataError errorMsg = null;
		List<LoseformBarcodeEntity> LoseformBarcodeList = new ArrayList<LoseformBarcodeEntity>();
		for (Map.Entry<Integer, List<Object>> entity : map.entrySet()) {
			List<Object> excelModelList = entity.getValue();
			if (ValidationUtil.isNullOrEmptyCollection(excelModelList)) {
		//		errorMsg = excelimportcontext.new DataError(InventoryConstantUtil.IMPORT_DATA_ERROR_TYPE_NOT_SHEET,
		//				"导入数据为空！",InventoryConstantUtil.IMPORT_DATA_ERROR_TYPE_NOT_SHEET);
		//		excelimportcontext.push(errorMsg);
			}else{
				//TODO 验证对应数据
				for (int i =0;i<excelModelList.size();i++) {
					LoseformBarcodeEntity LoseformBarcodeModel = (LoseformBarcodeEntity) excelModelList.get(i);
	 				if (checkDataByEntityModel(shShopModel, excelimportcontext, i)) {
						break;
					}
					//TODO 实体对象加入对应参数值 
					LoseformBarcodeModel = setEntityData(LoseformBarcodeModel,usrName, creatTime);
					LoseformBarcodeList.add(LoseformBarcodeModel);
				}
			}
		}
		return LoseformBarcodeList;
	}*/
	
	/**
	 * 
	 * 单个导入实体数据校验 有错误返回true
	 * @author weiwei
	 * @date 2017年3月28日 下午3:59:17
	 * @param eqEquipmentModel
	 * @param excelimportcontext
	 * @param i
	 * @return
	 */
	/*public boolean checkDataByEntityModel(LoseformBarcodeEntity LoseformBarcodeModel,ExcelImportContext excelimportcontext,int i) {
		if (checkSingleData(excelimportcontext, LoseformBarcodeModel.getIsDel, i+2,"错误信息") 
			//	|| checkSingleData(excelimportcontext, LoseformBarcodeModel.getEquipmentColor(), i+2, "颜色为空！")
			//	||checkSingleData(excelimportcontext, LoseformBarcodeModel.getEquipmentModel(), i+2, "型号为空！")
				) {
			return true;
		}else{
			return false;
		}
	}*/
	
	/**
	 * 
	 * 设置报错信息
	 * @author weiwei
	 * @date 2017年3月22日 下午5:17:37
	 * @param excelimportcontext
	 * @param rowNumId
	 * @param errorMessage
	 * @param sheetNumId 现在只有一个sheet 就不传值 默认值即可
	 */
	/*public boolean checkSingleData(ExcelImportContext excelimportcontext,String checkVal,int rowNumId,String errorMessage) {
		boolean flag = false;
		ExcelImportContext.DataError errorMsg = null;
		if (ValidationUtil.isNullOrEmptyString(checkVal)) {
			errorMsg = excelimportcontext.new DataError(rowNumId,errorMessage,InventoryConstantUtil.IMPORT_DATA_ERROR_TYPE_NOT_SHEET);
			excelimportcontext.push(errorMsg);
			flag = true;
		}
		return flag;
	}*/
	
	/**
	 * 
	 * 导入单个实体添加统一导入人及导入时间
	 * @author zhuxiaoliu
	 * @date 2017年3月28日 下午4:05:15
	 * @param eqEquipmentModel
	 * @param usrName
	 * @param creatTime
	 * @return
	 */
	/*public LoseformBarcodeEntity setEntityData(LoseformBarcodeEntity LoseformBarcodeModel,String usrName,String creatTime) {
		
		//LoseformBarcodeModel.setEquipmentId(InventoryConstantUtil.EQ_EQUIPMENT_SEQUENCE);
		LoseformBarcodeModel.setCreateTime(creatTime);
		LoseformBarcodeModel.setCreateUsrName(usrName);
		LoseformBarcodeModel.setLastUpdateUsrName(usrName);
		LoseformBarcodeModel.setLastUpdateTime(creatTime);
		LoseformBarcodeModel.setIsDel(SysConstant.IS_DEL_N);
		return LoseformBarcodeModel;
	}*/
	
	
	/**
	 * 
	 * 执行批插入操作
	 * @author weiwei
	 * @date 2017年3月13日 上午11:37:14
	 * @param {moduleName}List
	 * @return
	 */
	public ResponseEntity batchAddLoseformBarcode(List<LoseformBarcodeEntity> LoseformBarcodeList) {
		 ResponseEntity rs = new ResponseEntity(SysConstant.SUCCESS, "操作成功");
		try{
			int i = LoseformBarcodeDmlService.batchAddLoseformBarcode(LoseformBarcodeList);
			 rs.setMessage(i+"条记录插入操作成功");
		 }catch(AbstractGenericException e){
			logger.error(e.getFullMessage());
			rs.setStatus(SysConstant.FAIL).setMessage(e.getFullMessage());
		}catch(Exception e){
			String errorMessage = StringUtils.toString(e);
			logger.error(errorMessage);
			rs.setStatus(SysConstant.FAIL).setMessage(errorMessage);
		}
		return rs;
	}
	public List<BigInteger> insertData(Map<Integer, List<Object>> dataMap,
			ExcelImportContext context, String importType) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}

